package net.neptech.controller;

import lombok.RequiredArgsConstructor;
import net.neptech.bo.RoleBo;
import net.neptech.model.sys.Role;
import net.neptech.page.PageQuery;
import net.neptech.page.TableDataInfo;
import net.neptech.service.RoleService;
import net.neptech.vm.ResponseResult;
import net.neptech.vo.RoleVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: liyuxiang
 * @Date: 2025/8/14 22:18
 * @Description: TODO
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/role")
public class RoleController {
    @Autowired
    RoleService roleService;

    /**
     * 根据角色编号获取详细信息
     */
    @GetMapping(value = "/{roleId}")
    public ResponseResult<RoleVo> getInfo(@PathVariable Long roleId) {
        RoleVo roleVo = roleService.selectRoleById(roleId);
        return ResponseResult.success(roleVo);
    }

    /**
     * 获取角色列表
     *
     * @param pageQuery 分也查询
     * @return
     */
    @GetMapping("/list")
    public TableDataInfo<RoleVo> list(PageQuery pageQuery) {
        return roleService.selectPageRoleList(pageQuery);
    }

    /**
     * 修改角色状态
     *
     * @param roleVo 角色信息
     * @return
     */
    @PutMapping("/changeStatus")
    public ResponseResult<Void> changeStatus(@RequestBody RoleVo roleVo) {
        roleService.changeStatus(roleVo);
        return ResponseResult.success();
    }

    /**
     * 新增角色
     *
     * @param roleBo 角色信息
     * @return
     */
    @PostMapping("")
    public ResponseResult<Void> insertRole(@RequestBody RoleBo roleBo) {
        roleService.insertRole(roleBo);
        return ResponseResult.success();
    }

    /**
     * 更新角色
     *
     * @param roleBo 角色信息
     * @return
     */
    @PutMapping("")
    public ResponseResult<Void> updateRole(@RequestBody RoleBo roleBo) {
        roleService.updateRole(roleBo);
        return ResponseResult.success();
    }

    /**
     * 删除角色
     *
     * @param roleIds 角色id[]
     * @return
     */
    @DeleteMapping("{roleIds}")
    public ResponseResult<Void> deleteRoleByIds(@PathVariable("roleIds") Long[] roleIds) {
        roleService.deleteRoleByIds(roleIds);
        return ResponseResult.success();
    }

    @GetMapping("getAll")
    ResponseResult<List<Role>> getAllRole(){
        List<RoleVo> tmp = roleService.selectRoleAll();
        List<Role> res = new ArrayList<>();
        for (RoleVo roleVo : tmp) {
            Role role = new Role();
            BeanUtils.copyProperties(roleVo, role);
            res.add(role);
        }
        return ResponseResult.success(res);
    }
}
